﻿package 
{
	//import flash.errors.*;
	
	public class Tools
	{
		/**
		 * An equivalent of PHP's recursive print function print_r, which displays objects and arrays in a way that's readable by humans
		 * @param obj    Object to be printed
		 * @param level  (Optional) Current recursivity level, used for recursive calls
		 * @param output (Optional) The output, used for recursive calls
		 */
		public static function pr(obj:*, level:int = 0, output:String = ''):*
		{
			if (level == 0)
			{
				var tempError:Error = new Error();
				var stackTrace:String = tempError.getStackTrace();
				
				var parts:Array = stackTrace.split("\n");
				trace('Dump from: '+parts[2]);
			}
			
			if((typeof obj == 'string' || typeof obj == 'number') && level == 0)
			{
				trace(obj);
				return;
			}
			
			if (level == 0)
			{
				output = '(' + Tools.typeOf(obj) + ') {\n';
			}
			else if (level == 10)
			{
				return output;

			}
			var tabs:String = '\t';
			for (var i:int = 0; i < level; i++, tabs += '\t')
			{
			}
			for (var child:* in obj)
			{
				output+=tabs+'['+child+'] => ('+Tools.typeOf(obj[child])+') ';

				if (Tools.count(obj[child])==0)
				{
					output+=obj[child];
				}

				var childOutput:String='';
				if (typeof obj[child]!='xml')
				{
					childOutput=Tools.pr(obj[child],level+1);
				}
				if (childOutput!='')
				{
					output+='{\n'+childOutput+tabs+'}';
				}
				output+='\n';
			}

			if (level==0)
			{
				trace(output +'}\n');
			}
			else
			{
				return output;
			}
		}/**
		 * An extended version of the 'typeof' function
		 * @param 	variable
		 * @return	Returns the type of the variable
		 */

		public static function typeOf(variable:*):String
		{
			if (variable is Array)
			{
				return 'array';
			}
			else if (variable is Date)
			{
				return 'date';
			}
			else
			{
				return typeof variable;
			}
		}/**
		 * Returns the size of an object
		 * @param obj Object to be counted
		 */

		public static function count(obj:Object):uint
		{
			if (Tools.typeOf(obj)=='arrray')
			{
				return obj.length;
			}
			else
			{
				var len:uint=0;
				for (var item:* in obj)
				{
					if (item!='mx_internal_uid')
					{
						len++;
					}
				}
				return len;
			}
		}

		public static function formatTime($time:uint):String
		{
			var formattedTime:String;
			var hrs:uint;
			var mins:uint;
			var secs:uint;
			var ms:uint;

			var msAfterHrs:uint = $time % ((1000 * 60) * 60);
			hrs = ($time - msAfterHrs) / ((1000 * 60) * 60);

			var msAfterMins:uint = msAfterHrs % (1000 * 60);
			mins = (msAfterHrs - msAfterMins) / (1000 * 60);

			var msAfterSecs:uint=msAfterMins%1000;
			secs = (msAfterMins - msAfterSecs) / 1000;

			ms=msAfterSecs;

			if (ms==100)
			{
				ms=0;
			}

			//formattedTime = formatNumber(hrs, 2)+":"+formatNumber(mins, 2)+":"+formatNumber(secs, 2)+"."+formatNumber(ms, 3);
			formattedTime=formatNumber(hrs,2)+":"+formatNumber(mins,2)+":"+formatNumber(secs,2);

			return formattedTime;
		}

		public static function formatNumber($num:uint, $digits:uint):String
		{
			var formattedNum:String;

			if ($digits==2)
			{
				if ($num<10)
				{
					formattedNum="0"+$num;
				}
				else
				{
					formattedNum=String($num);
				}
			}
			else if ($digits == 3)
			{
				if ($num<10)
				{
					formattedNum="00"+$num;
				}
				else if ($num < 100)
				{
					formattedNum="0"+$num;
				}
				else
				{
					formattedNum=String($num);
				}
			}
			return formattedNum;
		}

		public static function unformatTime($time:String)
		{
			var time_parts=$time.split(':');
			var hrs:uint=time_parts[0];
			var mins:uint=time_parts[1];
			var secs:uint=time_parts[2];
			var unformattedTime:uint;
			trace(hrs);
			unformattedTime = hrs * ((1000 * 60) * 60);
			unformattedTime += mins * (1000 * 60);
			unformattedTime+=secs*1000;
			return unformattedTime;
		}

		public static function backtrace()
		{
			var tempError:Error = new Error();
			var stackTrace:String = tempError.getStackTrace();
			
			trace(stackTrace);
		}

	}
}